<template>
  <div>
    <Header></Header>
    <div class="content">
      <el-timeline>
        <el-timeline-item
          :timestamp="blog.created"
          placement="top"
          v-for="(blog, index) in blogs"
          :key="index"
        >
          <el-card>
            <h4>
              <router-link
                :to="{ name: 'BlogDetail', params: { blogid: blog.id } }"
              >
                {{ blog.title }}
              </router-link>
            </h4>
            <p>{{ blog.description }}</p>
            <div class="mbutton" v-if="blog.userId==currentUserId">
              <el-button
                type="primary"
                icon="el-icon-edit"
                circle
                @click="editBlog(blog.id)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                @click="deleteBlog(blog.id)"
              ></el-button>
            </div>
          </el-card>
        </el-timeline-item>
      </el-timeline>
    </div>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size.sync="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import Header from "../components/Header.vue";
export default {
  name: "blogs",
  components: { Header },
  data() {
    return {
      blogs: {},
      currentPage: 1,
      total: 0,
      pageSize: 5,
      currentUserId:""
    };
  },
  created() {
    this.getBlogs();
  },
  methods: {
    getBlogs() {
      var vm = this;
      vm.$axios({
        method: "get",
        url:
          "/blog/list?currentPage=" +
          vm.currentPage +
          "&pageSize=" +
          vm.pageSize,
      }).then((res) => {
        console.log(res.data.data.records);
        vm.blogs = res.data.data.records;
        vm.total = res.data.data.total;
        vm.currentUserId = vm.$store.getters.getUser.id;
      });
    },
    handleSizeChange(val) {
      // this.pageSize = val;
      this.getBlogs();
      console.log(this.pageSize);
    },
    handleCurrentChange(val) {
      // this.currentPage = val;
      console.log(this.currentPage);
      this.getBlogs();
    },
    editBlog(id) {
      this.$router.push("/blog/" + id + "/edit/");
    },
    deleteBlog(id) {
      var vm = this;
      vm.$axios({
        method: "post",
        url: "/blog/delBlog?blogId=" + id,
      }).then((res) => {
        var resdata = res.data;
        if (resdata.code === 200) {
          vm.$message({
            message: resdata.data,
            type: "success",
          });
          vm.getBlogs();
        } else {
          vm.$message.error(resdata.msg);
        }
      });
    },
  },
};
</script>
<style scoped>
.content {
  position: relative;
}
.block {
  margin: 0 auto;
  text-align: center;
}
.mbutton {
  text-align: right;
  position: absolute;
  top: 50%;
  right: 20px;
}
</style>